package com.nuoniu.sibanyun.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.finance.VVoucherTemplate;
import com.nuoniu.sibanyun.mapper.finance.VVoucherTemplateMapper;
import com.nuoniu.sibanyun.service.finance.IVVoucherTemplateDetailService;
import com.nuoniu.sibanyun.service.finance.IVVoucherTemplateService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 凭证模板表 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-12-18
 */
@Service
public class VVoucherTemplateServiceImpl extends ServiceImpl<VVoucherTemplateMapper, VVoucherTemplate> implements IVVoucherTemplateService {

    @Autowired
    private IVVoucherTemplateDetailService detailService;

    @Override
    public void saveTemplate(VVoucherTemplate template) {
        List<Long> detailIds = new ArrayList<>();
        UserInfo userInfo = UserThreadLocal.get();
        if(template.getId() == null) {
            template.setCompanyId(userInfo.getCompanyId());
            template.setCreateUserId(userInfo.getUserId());
        }

        this.saveOrUpdate(template);
        template.getVoucherTemplateDetailList().forEach(t -> {
            if(t.getId() == null) {
                t.setCompanyId(userInfo.getCompanyId());
                t.setOrderId(template.getId().intValue());
            } else {
                detailIds.add(t.getId());
            }
        });

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.eq("order_id", template.getId());
        if(detailIds.size() > 0) {
            wrapper.notIn("id", detailIds);
        }
        detailService.remove(wrapper);
        detailService.saveOrUpdateBatch(template.getVoucherTemplateDetailList());
    }

    @Override
    public Page<VVoucherTemplate> getList(Page page, String voucherType) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", UserThreadLocal.get().getCompanyId());
        if(StringUtils.isNotBlank(voucherType)) {
            wrapper.eq("voucher_type", voucherType);
        }

        Page<VVoucherTemplate> templatePage = page(page, wrapper);
        for(VVoucherTemplate template : templatePage.getRecords()) {
            QueryWrapper dWrapper = new QueryWrapper();
            dWrapper.eq("order_id", template.getId());
            template.setVoucherTemplateDetailList(detailService.list(dWrapper));
        }
        return templatePage;
    }

    @Override
    public VVoucherTemplate getDetail(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_id", id);
        VVoucherTemplate template = this.getById(id);
        template.setVoucherTemplateDetailList(detailService.list(wrapper));
        return template;
    }

    @Override
    public void delete(Long id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_id", id);
        this.removeById(id);
        detailService.remove(wrapper);
    }
}
